public class demo12 {
    
    public static void main(String[] args) {

        Object o1 = new Object();
        Object o2 = new Object();

        Thread t1 = new Thread(()->{
           synchronized (o1){
               //这里用到 sleep 方法的原因是因为保证 t2 线程执行完:对 o2 进行加锁操作
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }

               synchronized (o2){
                   System.out.println("正在执行 t1 线程");
               }
           }
        });

        Thread t2 = new Thread(()->{
           synchronized (o2){
               //这里用到 sleep 方法的原因是因为保证 t1 线程执行完:对 o1 进行加锁操作
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }

               synchronized (o1){
                   System.out.println("正在执行 t2 线程");
               }
           }
        });


        t1.start();
        t2.start();
    }
}
